﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class LoaiChuongTrinhDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from LoaiChuongTrinh");
        }

        public LoaiChuongTrinh GetById(String maloaiCT)
        {
            LoaiChuongTrinh lct = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from LoaiChuongTrinh where MaLoaiCT = '" + maloaiCT + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    lct = new LoaiChuongTrinh();
                    lct.MaLoaiCT = dataReader.GetString(0);
                    lct.TenLoaiCT = dataReader.GetString(1);
                    lct.Status = dataReader.GetBoolean(2);
                    return lct;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return lct;
        }

        public bool Insert(LoaiChuongTrinh lct)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[LoaiChuongTrinh]([MaLoaiCT],[TenLoaiCT][Status])"
               + "VALUES('" + lct.MaLoaiCT + "','" + lct.TenLoaiCT + "','" + lct.Status + "')";
            return Update(sql);
        }

        public bool DeleteById(String maloaiCT)
        {
            return Update("delete from LoaiChuongTrinh where MaLoaiCT = '" + maloaiCT + "'");
        }

        public bool UpdateById(LoaiChuongTrinh lct)
        {
            String sql = "update LoaiChuongTrinh "
                 + " set TenLoaiCT = '" + lct.TenLoaiCT + "'"
                 + " set Status = " + lct.Status + ""
                 + " where MaLoaiCT = '" + lct.MaLoaiCT + "'";
            return Update(sql);
        }
    }
}
